package com.lkhhd.ticket.serviceImpl;

import java.util.List;

import javax.persistence.Column;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lkhd.common.exception.TicketException;
import com.lkhd.common.exception.UserException;
import com.lkhd.common.myemum.TicketEnum;
import com.lkhd.common.myemum.UserEnum;
import com.lkhd.common.utils.IDUtil;
import com.lkhd.manage.mapper.ActivityMapper;
import com.lkhd.manage.mapper.ActivitymultipartcategoryMapper;
import com.lkhd.manage.mapper.ActivityuserplanMapper;
import com.lkhd.manage.mapper.AreasMapper;
import com.lkhd.manage.mapper.AttributevalueMapper;
import com.lkhd.manage.mapper.CategoryMapper;
import com.lkhd.manage.mapper.TicketMapper;
import com.lkhd.manage.mapper.TickettypeMapper;
import com.lkhd.manage.mapper.UsercommentMapper;
import com.lkhd.manage.pojo.Activity;
import com.lkhd.manage.pojo.Attributevalue;
import com.lkhd.manage.pojo.Ticket;
import com.lkhd.manage.pojo.User;
import com.lkhd.manage.pojo.Usercomment;
import com.lkhd.manage.povo.TicketVo;
import com.lkhd.ticket.service.TicketService;

import ch.qos.logback.core.joran.util.beans.BeanUtil;

@Service
@Transactional
public class TicketServiceImpl implements TicketService {
	
	@Autowired
	private ActivityMapper activityMapper;
	@Autowired
	private TickettypeMapper tickettypeMapper;
	@Autowired
	private TicketMapper ticketMapper;
	@Autowired
	private UsercommentMapper usercommentMapper;
	@Autowired
	private AttributevalueMapper attributevalueMapper;
	@Autowired
	private AreasMapper areasMapper;
	
	/**
	 * 购买门票(创建)并且提交活动所需信息
	 * @param ticket 门票实体
	 * @param attributeValues 活动所需信息集合
	 * @return int 0失败 1成功
	 */
	public int buyTicket(Ticket ticket,List<Attributevalue> attributeValues){
		// controller 要设置userId（登录用户id,自己的用户id，不然可能是别人的门票）
		ticket.setTicketid(IDUtil.getId());
		ticket.setActive(0);
		ticket.setStatus(0);
		//插入购买门票记录
		int i = ticketMapper.insert(ticket);
		
		//存储该张门票所要填写的额外信息
		if(i==1 && attributeValues != null) {
			for(Attributevalue attributevalue : attributeValues) {
				attributevalue.setAttributevalueid(IDUtil.getId());
				attributevalue.setTicketId(ticket.getTicketid());
				
				attributevalueMapper.insert(attributevalue);
			}
		}
		
		return i;
	}

	/**
	 * 支付门票
	 * @param ticketId 门票id
	 * @param user 当前登录用户
	 * @return 0失败 1成功
	 */
	public int pay(Long ticketId,User user){
		Ticket ticket = myTicket(ticketId,user);
		//如果查询不到订单
		if(ticket == null) {
			//不是你的订单或该订单不存在
			throw new TicketException(TicketEnum.NOMY);
		}
		
		ticket.setStatus(TicketEnum.HAVE_PAID.getCode());
		
		//有Selective后缀的 为null将不会使用
		return ticketMapper.updateByPrimaryKeySelective(ticket);
	}
	
	/**
	 * 查看电子票信息
	 * @param ticketId 门票id
	 * @param user 当前登录用户
	 * @return 门票VO对象(活动名,活动时间,活动地点,门票基本信息,票种)
	 */
	public TicketVo checkTicket(Long ticketId,User user){
		
		Ticket ticket = myTicket(ticketId,user);	
		
		if(ticket == null) {
			//门票不是你的或不存在
			throw new TicketException(TicketEnum.NOMY);
		}
		
		//门票VO对象
		TicketVo ticketVO = new TicketVo();
		
		//查询活动信息
		Activity activity = activityMapper.selectByPrimaryKey(ticket.getActivityid());
		
		//门票基本信息
		ticketVO.setActivityid(ticket.getTicketid());
		ticketVO.setName(ticket.getName());
		ticketVO.setPhone(ticket.getPhone());
		if(ticket.getActive()==0) {
			ticketVO.setActiveStr("有效票");
		}
		ticketVO.setActiveStr("无效票");
		ticketVO.setPrice(ticket.getPrice());
		//票种
		ticketVO.setTicketTypeName(tickettypeMapper.selectByPrimaryKey(ticket.getTickettypeid()).getName());
		ticketVO.setActivityName(activity.getName());
		ticketVO.setStartdate(activity.getStartdate());
		ticketVO.setEnddate(activity.getEnddate());
		//省市区转中文
		ticketVO.setProvince(areasMapper.selectByPrimaryKey(activity.getProvinceid()).getName());
		ticketVO.setCity(areasMapper.selectByPrimaryKey(activity.getCityid()).getName());
		ticketVO.setArea(areasMapper.selectByPrimaryKey(activity.getAreaid()).getName());
		//详细地址
		ticketVO.setAddrdesc(activity.getAddrdesc());
		
		return ticketVO;
	}
	
	/**
	 * 评论活动
	 * @param ticketId 门票id
	 * @param usercomment 评论实体
	 * @return 0失败 1成功
	 */
	public int commentActivity(Long ticketId,Usercomment usercomment){
		//controller层设置用户id
		Ticket ticket = new Ticket();
		ticket.setTicketid(ticketId);
		ticket.setUserid(usercomment.getUserid());
		
		int count = ticketMapper.selectCount(ticket);
		
		if(0 >= count) {
			//门票不是你的或不存在
			throw new TicketException(TicketEnum.NOMY);
		}
		
		usercomment.setCommentid(IDUtil.getId());
		
		return usercommentMapper.insert(usercomment);
	}
	
	/**
	 * 删除门票购买记录(只能删除已过期,和为支付的门票)
	 * @param ticketId 门票id
	 * @param user 当前登录用户
	 * @return true成功 false失败
	 */
	public boolean delTicket(Long ticketId,User user){
		
		Ticket ticket = myTicket(ticketId,user);	
		
		if(ticket == null) {
			//门票不是你的或不存在
			throw new TicketException(TicketEnum.NOMY);
		}
		
		//不是未支付和已过期的门票
		if(!(ticket.getStatus()==TicketEnum.UNPAID.getCode()||ticket.getStatus()==TicketEnum.HAVE_EXPIRED.getCode())) {
			//删除失败
			throw new TicketException(TicketEnum.DELFAIL);
		}
		//删除属性表中与该门票相关的记录
		Attributevalue attributevalue = new Attributevalue();
		attributevalue.setTicketId(ticket.getTicketid());
		attributevalueMapper.delete(attributevalue);
		
		return ticketMapper.deleteByPrimaryKey(ticket)==1 ? true:false;
	}
	
	/**
	 * 根据门票id和用户对象判断是不是自己的订单
	 * @param ticketId 门票id
	 * @param user 当前登录用户
	 * @return 门票实体
	 */
	public Ticket myTicket(Long ticketId,User user){
		Ticket ticket = new Ticket();
		ticket.setTicketid(ticketId);
		if(user==null) {
			throw new UserException(UserEnum.NOLOGIN);
		}
		ticket.setUserid(user.getUserid());
		
		return ticket = ticketMapper.selectByPrimaryKey(ticket);
	}
}
